package vy.org.app.page.upload;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
import org.vy.dal.dao.VyDataUploadJobDAO;
import org.vy.dal.dao.VyDataUploadJobDO;

import vy.org.app.common.AppConstants;
import vy.org.app.common.BaseAction;
import vy.org.app.common.Config;
import vy.org.app.common.DataTemplateEnum;
import vy.org.app.common.ModuleEnum;

public class UploadAction extends BaseAction {

	@Override
	protected ModuleEnum getModuleId() {
		return ModuleEnum.BULK_UPLOAD;
	}

	@Override
	public ActionForward executeAction(ActionMapping mapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) {

		try {
			UploadForm myForm = (UploadForm) actionForm;
			if(myForm == null){
				myForm = new UploadForm();
			}
			request.setAttribute("uploadForm", myForm);

			FormFile myFile = myForm.getTheFile();
			if (myFile != null && myFile.getFileName() != null
					&& !"".equals(myFile.getFileName())) {
				// Process the FormFile
				byte[] fileData = myFile.getFileData();

				VyDataUploadJobDO dto = new VyDataUploadJobDO();
				dto.setAppId("WEB_APP");
				dto.setFileName(myFile.getFileName());
				dto.setStatus(0); // 0=init,1=in progress,2=success,3=failure
				dto.setTemplateId(DataTemplateEnum.EASY_MEMBER_SUBSCRIPTION
						.getId());
				dto = VyDataUploadJobDAO.insert(dto);

				dto.setFilePathBefore(dto.getJobId() + "_Before.csv");
				dto.setFilePathAfter(dto.getJobId() + "_After.csv");

				FileWriter fw = new FileWriter(
						Config.getProperty("UPLOAD_PATH") + "/"
								+ dto.getFilePathBefore());
				fw.write(new String(fileData));
				fw.flush();
				fw.close();

				// Set status in progress
				dto.setStatus(1);
				dto = VyDataUploadJobDAO.update(dto);

				// Start processing
				DataUploader du = new DataUploader();
				UploadReport urpt = du.start(dto.getJobId(),
						Config.getProperty("UPLOAD_PATH") + File.separator + dto.getFilePathBefore(), 
						Config.getProperty("UPLOAD_PATH") + File.separator + dto.getFilePathAfter());
				dto.setRecordsFailed(urpt.getFailure());
				dto.setRecordsProcessed(urpt.getProcessed());
				dto.setRecordsSkipped(urpt.getSkipped());
				dto.setRecordsSuccess(urpt.getSuccess());

				// Update status
				if (urpt.isStatus()) {
					dto.setStatus(2);
				} else {
					dto.setStatus(3);
				}
				dto = VyDataUploadJobDAO.update(dto);
				request.setAttribute(AppConstants.REPORT, urpt);
			}

			List<VyDataUploadJobDO> lst = VyDataUploadJobDAO.findAll();
			request.setAttribute(AppConstants.RESULT, lst);

			return mapping.findForward("success");
		} catch (IOException ie) {
			request.setAttribute(AppConstants.EXCEPTION, ie);
			return mapping.findForward("failure");
		} catch (Exception e) {
			request.setAttribute(AppConstants.EXCEPTION, e);
			e.printStackTrace();
			return mapping.findForward("failure");
		}
	}

}
